package com.inspiration.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inspiration.dao.CategoryMapper;
import com.inspiration.entity.Category;
import com.inspiration.entity.R;
import com.inspiration.entity.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description
 * @Author Vincent.jiao
 * @Date 2023/10/24 11:55
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryMapper categoryMapper;


    @RequestMapping("all/one")
    public Object allOne() {
        return R.ok("list", categoryMapper.listOneCategory());
    }

    @RequestMapping("all/two")
    public Object allTwo(long pId) {
        return R.ok("list", categoryMapper.listTwoCategory(pId));
    }


    /**
     *
     * @param category
     */
    @RequestMapping("/insert")
    public void insert(Category category) {
        category.setC_create_time(new Date());
        category.setC_name(category.getC_name().trim());
        category.setC_desc(category.getC_desc().trim());

        categoryMapper.insert(category);
    }

    /**
     *
     * @param category
     */
    @RequestMapping("/update")
    public void update(Category category) {
        category.setC_name(category.getC_name().trim());
        category.setC_desc(category.getC_desc().trim());

        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(Category::getC_name, category.getC_name())
                .set(Category::getC_desc, category.getC_desc())
                .eq(Category::getC_id, category.getC_id());

        categoryMapper.update(category, updateWrapper);
    }

    /**
     * del
     */
    @RequestMapping("/del")
    public void del(Long cId    ) {
        categoryMapper.deleteById(cId);
    }

    /**
     * del
     */
    @RequestMapping("/count")
    public Object count(Long cId) {
        int childrenCount = categoryMapper.childrenCount(cId);
        int contentCount = categoryMapper.contentCount(cId);
        return R.ok("children", childrenCount).put("content", contentCount);
    }
}
